package com.ms.pda.activity

import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.StringUtils
import com.ms.architecture.base.DataBindingConfig
import com.ms.pda.BR
import com.ms.pda.R
import com.ms.pda.base.BaseActivity
import com.ms.pda.interf.OnDialogCallback
import com.ms.pda.messenger.AppMessenger
import com.ms.pda.messenger.SettingMessenger
import com.ms.pda.model.Constants
import com.ms.pda.utils.DialogUtil
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.param.Param

class SettingActivity : BaseActivity() {

    private var mMsger: SettingMessenger? = null
    private var mApp: AppMessenger? = null

    override fun initViewModel() {
        mMsger = getActivityScopeViewModel(SettingMessenger::class.java)
        mApp = getApplicationScopeViewModel(AppMessenger::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_setting, BR.state, mMsger!!)
            ?.addBindingParam(BR.listener, SettingListener())
            ?.addBindingParam(BR.app, mApp!!)
    }

    override fun initViews() {
        super.initViews()
        mApp!!.username.value = SPUtils.getInstance().getString(Constants.ACCOUNT, "")
    }

    open inner class SettingListener {
        fun logout(view: View) {
            DialogUtil.getTipPopupView(
                this@SettingActivity,
                getString(R.string.tip_logout_confirm),
                false,
                object : OnDialogCallback {
                    override fun onConfirm() {
                        SPUtils.getInstance().remove(Constants.LOGIN_INFO)
                        SPUtils.getInstance().remove(Constants.TENANT_DOMAIN)
                        RxHttpPlugins.init(RxHttpPlugins.getOkHttpClient())
                            .setDebug(Constants.DEBUG)
                            .setOnParamAssembly { param: Param<*>? -> param }
                        ActivityUtils.finishAllActivities()
                        ActivityUtils.startLauncherActivity()
                    }

                    override fun onCancel() {
                    }
                })
        }

    }
}